// pages/searchGoods/searchGoods.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        inputText: "",
        historyList: [],
        searchItemList: ["鸡蛋", "纸巾", "面包", "大米", "泡面", "玉米", "洗洁精", "牙膏", "酸奶", "水果", "啤酒", "苹果", "雨伞", "可乐", "荔枝", "洗衣液"],
        // 状态栏高度
        statusBarHeight: wx.getStorageSync('statusBarHeight') + 'px',
        statusBarPaddingTop: wx.getStorageSync('statusBarHeight') + 50 + 'px',
        // 导航栏高度
        navigationBarHeight: wx.getStorageSync('navigationBarHeight') + 'px',
        // 胶囊按钮高度
        menuButtonHeight: wx.getStorageSync('menuButtonHeight') + 'px',
        // 导航栏和状态栏高度
        navigationBarAndStatusBarHeight:
            wx.getStorageSync('statusBarHeight') +
            wx.getStorageSync('navigationBarHeight') +
            'px'
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },
    handleInputSearch() {
        if (!this.data.historyList) {
            this.setData({
                historyList: []
            })
        }
        let i = this.data.historyList.indexOf(this.data.inputText)
        if (i > -1) {
            this.data.historyList.splice(this.data.historyList.indexOf(this.data.inputText), 1)
        }
        this.data.historyList.unshift(this.data.inputText)
        wx.setStorageSync('youtxuan-history-search', this.data.historyList)
        wx.navigateTo({
            url: '/pages/goodsSearchList/goodsSearchList?keyword=' + this.data.inputText,
        })
    },
    handleInput(e) {
        this.setData({
            inputText: e.detail.value
        })
    },
    deleteAllHistory() {
        if (this.data.historyList.length > 0) {
            wx.showModal({
                content: '确定要删除历史记录吗？',
                cancelText: "再想想",
                confirmText: "删除",
                success: res => {
                    if (res.confirm) {
                        wx.removeStorageSync('youtxuan-history-search')
                        this.setData({
                            historyList: []
                        })
                    } else if (res.cancel) {
                    }
                }
            })
        }

    },
    backBtn() {
        wx.navigateBack({
            delta: 1,
        })
    },
    handleSearchAction(e) {
        if (!this.data.historyList) {
            this.setData({
                historyList: []
            })
        }
        let i = this.data.historyList.indexOf(e.currentTarget.dataset.keyword)
        if (i > -1) {
            this.data.historyList.splice(this.data.historyList.indexOf(e.currentTarget.dataset.keyword), 1)
        }
        this.data.historyList.unshift(e.currentTarget.dataset.keyword)
        wx.setStorageSync('youtxuan-history-search', this.data.historyList)
        wx.navigateTo({
            url: '/pages/goodsSearchList/goodsSearchList?keyword=' + e.currentTarget.dataset.keyword,
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.setData({
            historyList: wx.getStorageSync('youtxuan-history-search'),
            inputText: ""
        })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})